package com.zhkj.txg.module.home.adapter

import android.graphics.Paint
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.base.CustomApplication
import com.zhkj.txg.module.home.entity.HotGoodsHeaderEntity
import com.zhkj.txg.module.home.entity.SearchProductEntity
import com.zhkj.txg.module.home.entity.SellEntity
import com.zhkj.txg.module.product.ui.ProductDetailActivity2
import com.zhkj.txg.utils.ImageUtils
import kotlinx.android.synthetic.main.item_product_top_2.*


const val ITEM_SELL = 0
const val ITEM_HEADER = 1
const val ITEM_GOODS = 2
const val ITEM_DIVIDER_HEADER = 3
const val ITEM_RECOMMEND = 4
const val ITEM_PROMOTION = 5

class HomeSellGoodAdapter : BaseNodeAdapter(), LoadMoreModule {

    init {
        addFullSpanNodeProvider(SellNodeProvider())
        addFullSpanNodeProvider(HeaderNodeProvider())
        addFullSpanNodeProvider(DividerHeaderProvider())
        addNodeProvider(GoodsNodeProvider())
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        return when (data[position]) {
            is SellEntity -> ITEM_SELL
            is SearchProductEntity -> ITEM_GOODS
            is HotGoodsHeaderEntity -> ITEM_HEADER
            else -> ITEM_DIVIDER_HEADER
        }
    }
}

class SellNodeProvider : BaseNodeProvider() {
    override val itemViewType = ITEM_SELL
    override val layoutId: Int = R.layout.item_home_sell

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        with(data as SellEntity) {
            val progress = salesSum.toFloat() / (salesSum + storeCount) * 100
            val imageView = helper.setText(R.id.tvSellTitle, goodName)
                .setText(R.id.tvSellPrice, SpanUtil.setMoneySymbol(price, true))
                .setText(R.id.tvSellCount, String.format(context.getString(R.string.sell_count), salesSum))
                .setText(R.id.tvSellProgress, String.format(context.getString(R.string.sell_percent), progress.toInt()))
                .getView<ImageView>(R.id.ivSellThumb)
            ImageUtils.loadImage(context, imageView, image)
            helper.getView<ProgressBar>(R.id.pbSellProgress).progress = progress.toInt()
            // 设置是显示市场价还是显示分享赚多少钱
            helper.getView<TextView>(R.id.tvSellSpread).apply {
                if (AccountManager.isMember() && shareEarn.isNotEmpty() && shareEarn != "0") {
                    setTextColor(ContextCompat.getColor(context, R.color.colorMain))
                    paint.flags = Paint.ANTI_ALIAS_FLAG
                    text = SpanUtil.setPrefixMoneyStr(price = shareEarn)
                } else {
                    setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
                    paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                    paint.isAntiAlias = true
                    text = SpanUtil.setMoneySymbol(marketPrice)
                }
            }
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        ProductDetailActivity2.normalProduct(context, (data as SellEntity).id)
    }
}

class HeaderNodeProvider : BaseNodeProvider() {
    override val itemViewType = ITEM_HEADER
    override val layoutId: Int = R.layout.item_home_goods_header

    override fun convert(helper: BaseViewHolder, data: BaseNode) = Unit
}

class GoodsNodeProvider : BaseNodeProvider() {
    override val itemViewType = ITEM_GOODS
    override val layoutId: Int = R.layout.item_home_goods

    init {
    }

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        with(data as SearchProductEntity) {

            //是否活动价
            val isActivePrice: Boolean = !activePrice.isNullOrEmpty()
            //是否会员价
            val isPlusPrice: Boolean = !plusPrice.isNullOrEmpty() && activePrice.isNullOrEmpty()

            val tvGoodsPrice: TextView = helper.getView(R.id.tvGoodsPrice)
            val tvGoodsPrice2: TextView = helper.getView(R.id.tvGoodsPrice2)
            val llGoodsPush: LinearLayout = helper.getView(R.id.llGoodsPush)
            val tvGoodsPushPrice: TextView = helper.getView(R.id.tvGoodsPushPrice)
            val tvGoodsDiscount: TextView = helper.getView(R.id.tvGoodsDiscount)

            if (isActivePrice) {
                tvGoodsPrice.visibility = View.VISIBLE
                tvGoodsPrice2.visibility = View.VISIBLE
                tvGoodsDiscount.visibility = View.VISIBLE
                llGoodsPush.visibility = View.GONE
                tvGoodsPrice.text = SpanUtil.setMoneySymbol(activePrice, true)
                tvGoodsPrice2.text = SpanUtil.setMoneySymbol(price, false)
                tvGoodsPrice2.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                tvGoodsDiscount.text = "${discount}折"
            } else if (isPlusPrice) {
                tvGoodsPrice.visibility = View.VISIBLE
                tvGoodsPrice2.visibility = View.GONE
                tvGoodsDiscount.visibility = View.GONE
                llGoodsPush.visibility = View.VISIBLE
                tvGoodsPrice.text = SpanUtil.setMoneySymbol(price, false)
                tvGoodsPushPrice.text = SpanUtil.setMoneySymbol(plusPrice, false)
            } else {
                tvGoodsPrice.visibility = View.VISIBLE
                tvGoodsPrice2.visibility = View.GONE
                tvGoodsDiscount.visibility = View.GONE
                llGoodsPush.visibility = View.GONE
                tvGoodsPrice.text = SpanUtil.setMoneySymbol(price, false)
            }


//            if (isActivePrice) {
//                helper.setGone(R.id.tvGoodsPrice, false)
//                    .setGone(R.id.tvGoodsPrice2, false)
//                    .setGone(R.id.llGoodsPush, true)
//            } else {
//                helper.setGone(R.id.tvGoodsPrice, false)
//                    .setGone(R.id.tvGoodsPrice2, true)
//                    .setGone(R.id.llGoodsPush, true)
//            }

            helper.getView<ImageView>(R.id.ivGoodsThumb2).visibility = if (shopCount == 0) {
                View.VISIBLE
            } else {
                View.GONE
            }

            val imageView = helper.setText(R.id.tvGoodsTitle, goodsName)
                .getView<ImageView>(R.id.ivGoodsThumb)
//            if (activePrice.isNullOrEmpty()) {
//                helper.setGone(R.id.tvGoodsPrice2, true)
//                    .setGone(R.id.tvGoodsDiscount, true)
//                    .setText(R.id.tvGoodsPrice, SpanUtil.setMoneySymbol(price, false))
////                if (plusPrice.isNullOrEmpty()) {
////                    helper.setGone(R.id.tvGoodsPrice2, true)
////                        .setGone(R.id.tvGoodsDiscount, true)
////                        .setText(R.id.tvGoodsPrice, SpanUtil.setMoneySymbol(price, false))
////                } else {
////                    helper.setGone(R.id.tvGoodsPrice, true)
////                        .setGone(R.id.tvGoodsDiscount, true)
////                        .setGone(R.id.llGoodsPush, false)
////                        .setText(R.id.tvGoodsPushPrice, SpanUtil.setMoneySymbol(plusPrice, false))
////                }
//            } else {
//                helper.setGone(R.id.tvGoodsPrice2, false)
//                    .setGone(R.id.tvGoodsDiscount, false)
//                    .setGone(R.id.llGoodsPush, true)
//                    .setText(R.id.tvGoodsPrice, SpanUtil.setMoneySymbol(activePrice, false))
//                    .setText(R.id.tvGoodsPrice2, SpanUtil.setMoneySymbol(price, false))
//                    .setText(R.id.tvGoodsDiscount, "${discount}折")
//                val tvGoodsPrice2 = helper.getView<TextView>(R.id.tvGoodsPrice2)
//                tvGoodsPrice2.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
//            }
            ImageUtils.loadImage(context, imageView, image)
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        ProductDetailActivity2.normalProduct(context, (data as SearchProductEntity).id.toInt(), shopId = CustomApplication.getShopId())
    }

}

class DividerHeaderProvider : BaseNodeProvider() {
    override val itemViewType: Int = ITEM_DIVIDER_HEADER
    override val layoutId: Int = R.layout.item_home_sell_header
    override fun convert(helper: BaseViewHolder, data: BaseNode) = Unit
}